package cn.hxlc.tie.demo.modular.zr.material.service.impl;/*
 @author rui
 @date 2021/7/15 --19:03
*/

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hxlc.tie.demo.modular.zr.material.entity.MaterialGroup;
import cn.hxlc.tie.demo.modular.zr.material.mapper.MaterialGroupMapper;
import cn.hxlc.tie.demo.modular.zr.material.model.params.MaterialGroupParam;
import cn.hxlc.tie.demo.modular.zr.material.service.MaterialGroupService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.hxlc.tie.demo.core.constant.TreeConst.ROOT_ID;
import static cn.hxlc.tie.demo.core.constant.TreeConst.ROOT_PID;

@Service
@Transactional
public class MaterialGroupServiceImpl extends ServiceImpl<MaterialGroupMapper, MaterialGroup> implements MaterialGroupService {


    private MaterialGroup getEntity(MaterialGroupParam materialGroupParam) {
        MaterialGroup entity = new MaterialGroup();
        BeanUtil.copyProperties(materialGroupParam, entity);
        return entity;
    }
    private Serializable getKey(MaterialGroupParam param) {
        return param.getFid();
    }
    private MaterialGroup getOldEntity(MaterialGroupParam param) {
        return this.getById(getKey(param));
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Map<String, Object>> queryTree() {
        List<Map<String, Object>> treeMaps =
                this.listMaps(new QueryWrapper<MaterialGroup>().select("fid as nodeId", "fname as nodeName", "fparentid as nodePid")
                );
        //装配根节点
        Map<String, Object> rootNode = MapBuilder.create(new HashMap<String, Object>())
                .put("nodeId", ROOT_ID)
                .put("nodeName", "设备分组")
                .put("nodePid", ROOT_PID).build();
        treeMaps.add(rootNode);
        return treeMaps;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOne(MaterialGroupParam materialGroupParam) {
        MaterialGroup entity=new MaterialGroup();
        BeanUtil.copyProperties(materialGroupParam,entity);
        this.baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOne(MaterialGroupParam param) {
        MaterialGroup oldEntity = getOldEntity(param);
        MaterialGroup newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity, oldEntity);
        this.baseMapper.updateById(newEntity);
    }

    @Override
    public void deleteOne(Long fid) {
        this.baseMapper.deleteById(fid);
    }

    @Override
    public PageResult findPageBySpec(MaterialGroupParam param) {
        Page pageContext=getPageContext();
        IPage page=this.baseMapper.queryList(pageContext,param);
        return new PageResult<>(page);
    }

    private Page getPageContext(){
       return PageFactory.defaultPage();
    }
}
